package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

import javax.swing.JLabel;
import javax.swing.JTextField;

public class RandomTest extends JFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JButton button;
	private JButton button_1;
	private JButton btnLotto;
	private JButton btnLotto_1;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private JLabel lblNewLabel;
	private JTextField lu1;
	private JTextField lu2;
	private JTextField lu3;
	private JTextField lu4;
	private JTextField lu5;
	private JTextField lu6;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RandomTest frame = new RandomTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RandomTest() {
		setTitle("Random Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 486, 472);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getScrollPane());
		contentPane.add(getButton());
		contentPane.add(getButton_1());
		contentPane.add(getBtnLotto());
		contentPane.add(getBtnLotto_1());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getBtnNewButton_2());
		contentPane.add(getBtnNewButton_3());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLu1());
		contentPane.add(getLu2());
		contentPane.add(getLu3());
		contentPane.add(getLu4());
		contentPane.add(getLu5());
		contentPane.add(getLu6());
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 10, 446, 227);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("1~10");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int a = (int)(Math.random()*10)+1;
					result.append("1~10 : " + a + "\n");
				}
			});
			button.setBounds(12, 346, 97, 23);
		}
		return button;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("20~80");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int a = (int)(Math.random()*61)+20;
					result.append("20~80 : " + a + "\n");
					
					
				}
			});
			button_1.setBounds(121, 346, 97, 23);
		}
		return button_1;
	}
	private JButton getBtnLotto() {
		if (btnLotto == null) {
			btnLotto = new JButton("Lotto");
			btnLotto.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int[] lo = new int[6];
					for(int i=0;i<lo.length;i++){
						lo[i]=(int)(Math.random()*45+1);
						for(int j=0;j<i;j++){
							if(lo[j]==lo[i]) i=0;
						}
					}
					Arrays.sort(lo);
					result.append(Arrays.toString(lo)+"\n");
					
				}
			});
			btnLotto.setBounds(12, 379, 97, 23);
		}
		return btnLotto;
	}
	private JButton getBtnLotto_1() {
		if (btnLotto_1 == null) {
			btnLotto_1 = new JButton("Lotto2");
			btnLotto_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int[] base = new int[45];
					int[] lo = new int[6];
					int temp=0;
					for(int i=1;i<46;i++) base[i-1]=i;
					for(int i=1;i<1000;i++){
						int k=(int)(Math.random()*44)+1;
						temp = base[0];
						base[0]=base[k];
						base[k]=temp;
					}
					System.arraycopy(base, 0, lo, 0, 6);
					Arrays.sort(lo);
					result.append(Arrays.toString(lo));
					result.append("\n");
					
				}
			});
			btnLotto_1.setBounds(121, 379, 97, 23);
		}
		return btnLotto_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("1~45");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int a = (int)(Math.random()*45)+1;
					result.append("1~45 : " + a + "\n");
				}
			});
			btnNewButton.setBounds(230, 346, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("Clear");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText("");
				}
			});
			btnNewButton_1.setBounds(339, 346, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("Lucky Lotto");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// 랜덤과 중복처리과정이 있기 때문에 시간이 많이 걸릴지도 모른다.
					// 그래서 1~45가 들어있는 배열을 만들고
					// 행운의 숫자로 받은 수 들을 배열의 맨 앞으로 옮기고 그 나머지 뒷 부분을 섞어서
					// 배열의 앞 6개를 출력해주는 프로그램이 안정적이고 구현하기가 편하다.
					//
					
					
					int[] lo = new int[6];
					int pos=0;
										
					if(!lu1.getText().equals("") && Integer.parseInt(lu1.getText())>=1 && Integer.parseInt(lu1.getText())<=45){
						lo[pos]=Integer.parseInt(lu1.getText());
						pos++;
					}
					if(!lu2.getText().equals("") && Integer.parseInt(lu2.getText())>=1 && Integer.parseInt(lu2.getText())<=45){
						lo[pos]=Integer.parseInt(lu2.getText());
						pos++;
					}
					if(!lu3.getText().equals("") && Integer.parseInt(lu3.getText())>=1 && Integer.parseInt(lu3.getText())<=45){
						lo[pos]=Integer.parseInt(lu3.getText());
						pos++;
					}
					if(!lu4.getText().equals("") && Integer.parseInt(lu4.getText())>=1 && Integer.parseInt(lu4.getText())<=45){
						lo[pos]=Integer.parseInt(lu4.getText());
						pos++;
					}
					if(!lu5.getText().equals("") && Integer.parseInt(lu5.getText())>=1 && Integer.parseInt(lu5.getText())<=45){
						lo[pos]=Integer.parseInt(lu5.getText());
						pos++;
					}
					if(!lu6.getText().equals("") && Integer.parseInt(lu6.getText())>=1 && Integer.parseInt(lu6.getText())<=45){
						lo[pos]=Integer.parseInt(lu6.getText());
						pos++;
					}					
					
					result.append("입력받은 행운의 수 : "+pos+"개\n");				
					
					for(int i=0;i<pos-1;i++){
						for(int j=i+1;j<pos;j++){
							if (lo[i] == lo[j]) // 랜덤으로 나온 수가 같은게 발생한다면 
							{
								lo[j] = (int) (Math.random()*45 + 1);
								j = i; // 처음부터 다시 검사 
							}
						}
					}
										
					for(int i=pos;i<lo.length;i++){
						lo[i]=(int)(Math.random()*45+1);
						for(int j=0;j<i;j++){
							if(lo[j]==lo[i]) {
								lo[j]=(int)(Math.random()*45+1);
								i=pos;
							}
						}
					}					
					Arrays.sort(lo);
					result.append(Arrays.toString(lo)+"\n");
				}
			});
			btnNewButton_2.setBounds(230, 379, 97, 23);
		}
		return btnNewButton_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("Lotto3");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// 입력값이 공백이 아닌경우 1~45범위값인지 체크
					int pos=0; // 행운의 수가 대입 될 위치
					int[] base = new int[45];
					int[] lo = new int[6];
					for(int i=1;i<46;i++) base[i-1]=i;
					
					String[] lu = {
							lu1.getText(),lu2.getText(),
							lu3.getText(),lu4.getText(),
							lu5.getText(),lu6.getText()
							};
					//중복수 체크
					for(int i=0;i<lu.length;i++){
						for(int j=i+1;j<lu.length-1;j++){
							if(!lu[i].equals("") && lu[i].equals(lu[j])){
								JOptionPane.showMessageDialog(null, "중복된 숫자가 있습니다.");
								return;
							}
						}
					}
					
					try{
						//행운의 수를 배열의 맨 앞으로 이동
						for (int i=0;i<lu.length;i++){							
							if (!lu[i].equals("")){
								int temp = Integer.parseInt(lu[i]);
								if(temp>0 && temp<46){
									lo[pos]=temp;
									base[temp-1] = 99;									
									pos++;
								}else{
									Exception ee = new Exception("1~45까지만 가능."); // 여기에 입력된 메세지는 의미가 없다. ShowDialog 로 메세지를 출력했기때문.
									throw ee;
									/*JOptionPane.showMessageDialog(null, "숫자 1~45까지만 입력하세요.");									
									return;*/
								}
							}
						}
						// 행운의 숫자를 제외한 나머지를 섞음
						Arrays.sort(base);
						for(int i=1;i<1000;i++){
							int k=(int)(Math.random()*(44-pos))+1;
							int temp = base[0];
							base[0]=base[k];
							base[k]=temp;
						}
						System.arraycopy(base, 0, lo, pos, 6-pos);
						Arrays.sort(lo);
						result.append(Arrays.toString(lo)+"\n");
						result.setCaretPosition(result.getText().length());
						
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "숫자 1~45까지만 입력하세요.");
					}
					
				}
			});
			btnNewButton_3.setBounds(339, 379, 97, 23);
		}
		return btnNewButton_3;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Lucky Number");
			lblNewLabel.setBounds(12, 247, 97, 15);
		}
		return lblNewLabel;
	}
	private JTextField getLu1() {
		if (lu1 == null) {
			lu1 = new JTextField();
			lu1.setBounds(12, 272, 57, 21);
			lu1.setColumns(10);
		}
		return lu1;
	}
	private JTextField getLu2() {
		if (lu2 == null) {
			lu2 = new JTextField();
			lu2.setColumns(10);
			lu2.setBounds(81, 272, 57, 21);
		}
		return lu2;
	}
	private JTextField getLu3() {
		if (lu3 == null) {
			lu3 = new JTextField();
			lu3.setColumns(10);
			lu3.setBounds(150, 272, 57, 21);
		}
		return lu3;
	}
	private JTextField getLu4() {
		if (lu4 == null) {
			lu4 = new JTextField();
			lu4.setColumns(10);
			lu4.setBounds(219, 272, 57, 21);
		}
		return lu4;
	}
	private JTextField getLu5() {
		if (lu5 == null) {
			lu5 = new JTextField();
			lu5.setColumns(10);
			lu5.setBounds(288, 272, 57, 21);
		}
		return lu5;
	}
	private JTextField getLu6() {
		if (lu6 == null) {
			lu6 = new JTextField();
			lu6.setColumns(10);
			lu6.setBounds(357, 272, 57, 21);
		}
		return lu6;
	}
}
